﻿var ExtendedWiki;
(function (ExtendedWiki) {
    var InsertCodeSnipped = (function () {
        function InsertCodeSnipped() {
            this.insertMarker();
            $('.modal').on('hide.bs.modal', this.insertCodeInPlaceHolder);

            context = this;

            window.document.getElementById("Ew-CodePreviewInput").onkeyup = function () {
                context.updateCodePreview();
            };
        }
        InsertCodeSnipped.prototype.insertMarker = function () {
            if ($("#ms-rterangecursor-start").length > 0) {
                $(".Ew-codePlaceHolder").remove();
                $("<span class='Ew-codePlaceHolder' />").insertBefore("#ms-rterangecursor-start");
            }
        };

        InsertCodeSnipped.prototype.updateCodePreview = function () {
            $('#Ew-CodePreviewInput').autosize();

            var codePreviewInput = window.document.getElementById("Ew-CodePreviewInput").value;

            $(".Ew-CodePreview code").text(codePreviewInput);

            var codeType = $(".Ew-CodeTypeButtons").find("label[class*='active']").text().trim();

            switch (codeType) {
                case "HTML":
                    $(".Ew-CodePreview code").removeAttr('class').attr('class', 'xml');
                    break;

                case "CSS":
                    $(".Ew-CodePreview code").removeAttr('class').attr('class', 'css');
                    break;

                case "JavaScript":
                    $(".Ew-CodePreview code").removeAttr('class').attr('class', 'javascript');
                    break;

                case "C#":
                    $(".Ew-CodePreview code").removeAttr('class').attr('class', 'cs');
                    break;

                default:
                    $(".Ew-CodePreview code").removeAttr('class').attr('class', '');
            }

            $('pre code').each(function (i, e) {
                hljs.highlightBlock(e);
            });
        };

        InsertCodeSnipped.prototype.insertCodeInPlaceHolder = function () {
            if ($('.Ew-codePlaceHolder').length > 0) {
                var highlightedCode = $("<pre>" + $('.Ew-CodePreview').html() + "</pre>");

                var replacements = { "{{highlightedCode}}": highlightedCode, "{{Title}}": "Title" };

                var codeTemplate = "<div class='panel panel-default'>" + "<div class='panel-heading' >" + "<h3 class='panel-title'>{{Title}}</h3 >" + "</div>" + "<div class='panel-body'>" + "{{highlightedCode}}" + "</div>" + "</div>";

                codeTemplate = codeTemplate.replace(/{{\w+}}/g, function (all) {
                    return replacements[all] || all;
                });

                $(codeTemplate).insertBefore(".Ew-codePlaceHolder");
                $('.Ew-codePlaceHolder').remove();
            }
        };
        return InsertCodeSnipped;
    })();
    ExtendedWiki.InsertCodeSnipped = InsertCodeSnipped;
})(ExtendedWiki || (ExtendedWiki = {}));
;

var a = new ExtendedWiki.InsertCodeSnipped();
